﻿using System;
using System.ComponentModel;


// Modified based on code in this article: http://www.gbogea.com/2010/01/02/mvvm-multiselect-listbox
// Written by:  Gabriel Perez 

namespace OpenLightGroup.Common
{
    /// <summary>
    /// Object wrapper that allows tracking of if the item is selected.
    /// </summary>
    /// <typeparam name="T">Type of the Item you are wrapping</typeparam>
    public class SelectableItem<T> : INotifyPropertyChanged
    {

        #region Fields

        private bool isSelected;

        private T item;

        #endregion

        #region ctor

        /// <summary>
        /// Creates a new instance of SelectableItem
        /// </summary>
        /// <param name="item">Item to wrap</param>
        public SelectableItem(T item)
            : this(false, item)
        {
        }

        /// <summary>
        /// Creates a new instance of SelectableItem
        /// </summary>
        /// <param name="item">Item to wrap</param>
        /// <param name="selected">Default value for the selected property</param>
        public SelectableItem(bool selected, T item)
        {
            this.isSelected = selected;
            this.item = item;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Is the item selected.
        /// </summary>
        public bool IsSelected
        {
            get { return isSelected; }
            set
            {
                if (value != isSelected)
                {
                    isSelected = value;
                    this.PropertyChanged.Raise(this, "IsSelected");
                    this.SelectionChanged.Raise(this);
                }
            }
        }

        /// <summary>
        /// Item that is wrapped by this class
        /// </summary>
        public T Item
        {
            get { return item; }
            set
            {
                if (!value.Equals(item))
                {
                    item = value;
                    this.PropertyChanged.Raise(this, "Item");
                }
            }
        }

        #endregion

        #region Events

        /// <summary>
        /// Satisfies the INotifyPropertyChanged contract.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Raised when the item is selected or unselected.
        /// </summary>
        public event EventHandler SelectionChanged;

        #endregion

        //#region Event invokers

        //private void OnPropertyChanged(string propertyName)
        //{
        //    PropertyChangedEventHandler changed = PropertyChanged;
        //    if (changed != null)
        //        changed(this, new PropertyChangedEventArgs(propertyName));
        //}

        //private void OnSelectionChanged()
        //{
        //    EventHandler changed = SelectionChanged;
        //    if (changed != null)
        //        changed(this, EventArgs.Empty);
        //}

        //#endregion

    }


}
